export default {
  //板块
  forwordBk(plate_id) {
    if (plate_id <= 1) return;
    let url = 'https://xuangubao.cn/theme/' + plate_id;
    window.open(url);
  },

  //选股宝 11,SS上海，12,SZ深圳 财联社 SH上海，SZ深圳
  //格式化股票代码
  formatGu(stock, type = 'xuangubao') {
    //变成纯数字
    stock = stock.replace(/(\D+)/gi, '');
    let loc = '';
    if (stock.startsWith('6') || stock.startsWith('11')) {
      loc = type == 'xuangubao' ? 'SS' : 'SH';
    } else if (stock.startsWith('8')) {
      loc = 'BJ';
    } else {
      loc = 'SZ';
    }
    if (type == 'xuangubao') {
      return stock + '.' + loc;
    } else {
      return loc.toLocaleLowerCase() + stock;
    }
  },
  //格式化财联社code变成选股宝的，用于指数查询
  formatCls(code) {
    if (code.startsWith('sh')) {
      return code.replace(/(\D+)/gi, '') + '.SS';
    } else if (code.startsWith('sz')) {
      return code.replace(/(\D+)/gi, '') + '.SZ';
    } else {
      console.log(code, ' error');
      return '';
    }
  },
  forwordGu(stock) {
    stock = this.formatGu(stock);
    // let url = 'https://xuangubao.cn/stock/' + stock
    let url = 'http://www.iwencai.com/unifiedwap/result?w=' + stock + '&querytype=stock';
    window.open(url);
  },
  //格式化时间戳
  formatDateTime(UnixTime, showSecond = true) {
    if (UnixTime == 0) return '';
    var date = new Date(UnixTime * 1000);
    var h = date.getHours();
    h = h < 10 ? '0' + h : h;
    var minute = date.getMinutes();
    var second = date.getSeconds();
    minute = minute < 10 ? '0' + minute : minute;
    second = second < 10 ? '0' + second : second;
    if (showSecond) {
      return h + ':' + minute + ':' + second;
    }
    return h + ':' + minute;
  },
  formatDate(UnixTime) {
    if (UnixTime == 0) return '';
    var date = new Date(UnixTime * 1000);
    return date.toLocaleDateString();
  },
  //对数组进行排序
  compare(property) {
    return (firstobj, secondobj) => {
      const firstValue = firstobj[property];
      const secondValue = secondobj[property];
      return secondValue - firstValue; //降序
    };
  },
  objectSort(arr, fields) {
    let checkOrder = (left, right, fields) => {
      let field = fields.shift();
      if (field === undefined) return 0;
      if (field.method == 'asc' && field.type == 'zh') {
        return left[field.key].localeCompare(right[field.key], 'zh-CN');
      } else if (field.method == 'asc') {
        //升序
        return left[field.key] === right[field.key] ? checkOrder(left, right, fields) : left[field.key] < right[field.key] ? -1 : 1;
      } else if (field.method == 'des') {
        return left[field.key] === right[field.key] ? checkOrder(left, right, fields) : left[field.key] > right[field.key] ? -1 : 1;
      }
    };
    return arr.sort((left, right) => {
      return checkOrder(left, right, JSON.parse(JSON.stringify(fields)));
    });
  },
  creatX(fund) {
    let onehours = ['09', '10', '11', '13', '14'];
    let timegroup = [];
    onehours.forEach((item) => {
      if (item == '11') {
        for (let i = 0; i < 30; i++) {
          let str = item + ':' + (i < 10 ? '0' + i : i);
          timegroup.push(str);
        }
      } else if (item == '09') {
        for (let i = 30; i < 60; i++) {
          let str = item + ':' + i;
          timegroup.push(str);
        }
      } else {
        for (let i = 0; i < 60; i++) {
          let str = item + ':' + (i < 10 ? '0' + i : i);
          timegroup.push(str);
        }
      }
    });
    if (fund) {
      for (let i = 15; i < 30; i++) {
        let str = '09:' + i;
        timegroup.push(str);
      }
    }
    timegroup.push('15:00');
    return timegroup;
  },

  bigNumberTransform(value, type, len = 2) {
    const newValue = ['', '', ''];
    let fr = 1000;
    let num = 3;
    let text1 = '';
    let text2 = '';
    let fm = 1;
    if (value == '' || value == null || isNaN(value)) {
      return !type ? newValue : '';
    }
    if (value < 0) {
      value = Math.abs(value);
      text2 = '-';
    }
    while (value / fr >= 1) {
      fr *= 10;
      num += 1;
      // console.log('数字', value / fr, 'num:', num)
    }
    if (num <= 4) {
      // 千
      newValue[0] = value;
      newValue[1] = '';
    } else if (num <= 8) {
      // 万
      // text1 = parseInt(num - 4) / 3 > 1 ? '千万' : '万'
      text1 = '万';
      // tslint:disable-next-line:no-shadowed-variable
      fm = text1 === '万' ? 10000 : 10000000;
      if (value % fm === 0) {
        newValue[0] = parseInt(value / fm) + '';
      } else {
        newValue[0] = parseFloat(value / fm).toFixed(len) + '';
      }
      newValue[1] = text1;
    } else {
      // 亿 if (num <= 16)
      // text1 = (num - 8) / 3 > 1 ? '千亿' : '亿'
      text1 = '亿';
      text1 = (num - 8) / 4 > 1 ? '万亿' : text1;
      text1 = (num - 8) / 7 > 1 ? '千万亿' : text1;
      text1 = (num - 8) / 10 > 1 ? '亿亿' : text1;
      // tslint:disable-next-line:no-shadowed-variable
      fm = 1;
      if (text1 === '亿') {
        fm = 100000000;
      } else if (text1 === '千亿') {
        fm = 100000000000;
      } else if (text1 === '万亿') {
        fm = 1000000000000;
      } else if (text1 === '千万亿') {
        fm = 1000000000000000;
      } else {
        fm = 1000000000000000000;
      }
      if (value % fm === 0) {
        newValue[0] = parseInt(value / fm) + '';
      } else {
        newValue[0] = parseFloat(value / fm).toFixed(len) + '';
      }
      newValue[1] = text1;
    }
    if (value < 1000) {
      newValue[0] = value + '';
      newValue[1] = '元';
    }
    newValue[0] = text2 ? text2 + newValue[0] : newValue[0];
    return !type ? newValue : newValue[0] + newValue[1];
  },
};
